package com.geovoice;

//import java.util.ArrayList;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
//
//import android.app.Activity;
import android.app.ListActivity;
//import android.content.ComponentName;
//import android.content.Context;
import android.content.Intent;
//import android.content.ServiceConnection;
//import android.database.Cursor;
//import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
//import android.os.IBinder;
//import android.os.RemoteException;
//import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

/*    This file is part of Geovoice.

Geovoice is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Geovoice is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Geovoice.  If not, see <http://www.gnu.org/licenses/>.  */

public class DeleteLocation extends ListActivity{
	
	String TAG = "GEOVOICE - DeleteLocation";

	
//	
//	GVLServiceInterface mIRemoteService;
//	private ServiceConnection mConnection = new ServiceConnection() {
//	    // Called when the connection with the service is established
//	    public void onServiceConnected(ComponentName className, IBinder service) {
//	        // Following the example above for an AIDL interface,
//	        // this gets an instance of the IRemoteInterface, which we can use to call on the service
//	        mIRemoteService = GVLServiceInterface.Stub.asInterface(service);
//	        
//	        
//		        /*
//			  	String[] locationsBundleAr = new String[0];
//			  	  
//	//		  	  if(bundle!=null)
//	//		  	 	 locationsBundleAr =  bundle.getStringArray("locations");
//	//		  	  
//			  	Set<String> l = new HashSet<String>();
//			  	  
//			  	try {
//			  		LocationPhoneEnablePreference.getLocations(this);
//			  		locationsBundleAr=mIRemoteService.getLocations();
//			  	} catch (RemoteException e1) {
//			  		e1.printStackTrace();
//			  	}
//			  	  try{
//			  		  l.add("\u2295 Add Location");
//			  		  for(int i=0;i<locationsBundleAr.length;i++){
//			  			 if(!locationsBundleAr.equals("Elsewhere")){
//			  				 l.add(locationsBundleAr[i]);
//			  			 }
//			  		  }
//			  	  }catch(Exception e){
//			  		  e.printStackTrace();
//			  	  }
//			  	  
//		  	  
//		  	//  l.add("Elsewhere");
//		  	  
//		  	  
//		  	  
//		  	  try{
//		  		  String[] locations = l.toArray(new String[l.size()]);
//		  			 
//		  		  if(locations!=null)
//		  			  LOCATIONS = locations;
//		  	  }catch(Exception e){
//		  		  e.printStackTrace();
//		  	  }
//		  	  setListAdapter(new ArrayAdapter<String>(DeleteLocation.this,
//		  	          android.R.layout.simple_list_item_1, LOCATIONS));
//
//		  	  getListView().setTextFilterEnabled(true);
//		  	  
//		        
//	        */
//	        
//	    }
//
//	    // Called when the connection with the service disconnects unexpectedly
//	    public void onServiceDisconnected(ComponentName className) {
//	        Log.e(TAG, "Service has unexpectedly disconnected");
//	        mIRemoteService = null;
//	    }
//	};
	
	String[] LOCATIONS ;
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
//		
//	    Intent hello_service = new Intent(this, LocationService.class);
//	    
//		bindService( hello_service, mConnection,Context.BIND_AUTO_CREATE);
//		
		
		
		
		
		this.setContentView(R.layout.deletelocation);
		// ScrollView sv = new ScrollView(this);
	      //  LinearLayout ll = new LinearLayout(this);
	     //   ll.setOrientation(LinearLayout.VERTICAL);
	      //  sv.addView(ll);
		
	        
	    LinearLayout outerlayout= (LinearLayout)  this.findViewById(R.id.deletelocation);
	        
//	    SQLiteDatabase sql =  openOrCreateDatabase("db",0,null);
//	    try{
//	    	Log.e("GEOVOICE","DeleteLocation Attempt to get locationName");
//	    	locations = getStringArSQLVertical("SELECT locationName FROM LOCATIONS WHERE locationName != 'Elsewhere';",sql);
//	    }catch(Exception y){y.printStackTrace();}
//	    sql.close();
//	    
	    if(LOCATIONS!=null){
	    	
	    
	    	
	    	for(int i=0;i<LOCATIONS.length;i++){
	    		LinearLayout ll = new LinearLayout(this);
	    		ll.setOrientation(LinearLayout.VERTICAL);
	    		TextView loc = new TextView(this);
	    		loc.setText(LOCATIONS[i]);
	    		loc.setTextSize(23);
	    		
	    		TextView spacer = new TextView(this);
	    		spacer.setHeight(1);
	    		spacer.setWidth(android.view.ViewGroup.LayoutParams.FILL_PARENT);
	    		spacer.setBackgroundColor(Color.parseColor("#444444"));
	    		//loc.setLayoutParams(params)
	    		LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
	    		
	    		LayoutParams loclp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
	    		//loclp.setMargins(8, 16, 0, 16);	    		
	    		loc.setLayoutParams(loclp);
	    		loc.setPadding(16, 16, 0, 16);
	    		ll.setLayoutParams(lp);
	    		ll.addView(loc);
	    		ll.addView(spacer);
	    		outerlayout.addView(ll);
	    		
	    		final String finalLocationName = LOCATIONS[i];
	    		
	    		ll.setOnClickListener(destroyClick(finalLocationName));
	    	}
	    }else{
    		LinearLayout ll = new LinearLayout(this);
    		ll.setOrientation(LinearLayout.VERTICAL);
    		TextView loc = new TextView(this);
    		loc.setText("No Locations Set");
    		loc.setTextSize(23);
    		
    		
    		TextView spacer = new TextView(this);
    		spacer.setHeight(1);
    		spacer.setWidth(android.view.ViewGroup.LayoutParams.FILL_PARENT);
    		spacer.setBackgroundColor(Color.parseColor("#444444"));
    		//loc.setLayoutParams(params)
    		LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    		
    		LayoutParams loclp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    		//loclp.setMargins(8, 8, 0, 8);
    		loc.setPadding(8, 8, 0, 8);
    		loc.setLayoutParams(loclp);
    		ll.setLayoutParams(lp);
    		ll.addView(loc);
    		ll.addView(spacer);
    		outerlayout.addView(ll);
    		ll.setOnClickListener(closeClick);
	    }
	    
	    
	    //ADD CLICK LISTENERS
	    
	    
	    
	    
	      //  this.setContentView(layoutResID)
	}
	
	OnClickListener closeClick = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			DeleteLocation.this.finish();
			
		}
		
	};
	
	 OnClickListener destroyClick(final String locationName){
		
		OnClickListener ret = new OnClickListener(){
		

			@Override
			public void onClick(View v) {
				try{
					
					Bundle b = new Bundle();
					b.putString(Settings.LOCATION_NAME_EXTRA/*"locationName"*/, locationName);
					Intent i = new Intent(DeleteLocation.this,ConfirmDelete.class);
					i.putExtras( b);
					DeleteLocation.this.startActivity(i);
				
					DeleteLocation.this.finish();

					
//					
//					SQLiteDatabase sql = openOrCreateDatabase("db",0,null);
//					SQLHelper.
//					sql.execSQL("DELETE FROM LOCATIONS WHERE locationName = '"+locationName+"';");
//					sql.close();
				}catch(Exception e){
					e.printStackTrace();
				}
				//ADD MORE SHIT LATER
				DeleteLocation.this.finish();
			}
			
		};
		
		return ret;
	}
	
	
	
	
//	
//	private String[] getStringArSQLVertical(String query, SQLiteDatabase sql){
//		String[] ret = new String[0];
//		try{
//			Cursor c = sql.rawQuery(query, null);
//			List<String> list = new ArrayList<String>();
//			if(c!=null){
//				while(c.moveToNext()){
//					list.add(c.getString(0));
//					Log.e("GEOVOICE","DIRECT FROM QUERY +"+c.getString(0));
//				}
//				if(list.size()>0){
//					ret = list.toArray(new String[list.size()]);
//					//for(int i=0;i<list.size();i++){
//					//	ret[i] = list.get(i);
//					//}
//				}
//				c.close();
//			}
//			
//
//		}catch(Exception o){
//			o.printStackTrace();
//		}
//		return ret;
//	}
//	
//	

	
	@Override 
	public void onResume(){
		super.onResume();
//		
//	    Intent hello_service = new Intent(this, LocationService.class);
//	    
//		bindService( hello_service, mConnection,Context.BIND_AUTO_CREATE);
	}

	@Override
	public void onPause(){
//		try{
//			unbindService(mConnection);
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		super.onPause();
		this.finish();
	}
	
//	@Override
//	public void onDestroy(){
//		try{
//			unbindService(mConnection);
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		super.onDestroy();
//	}		
	
	
	
}
